package com.easidea.tim.service.saleorder;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springside.core.service.EntityService;

import com.easidea.tim.dao.saleorder.SaleOrderItemDao;
import com.easidea.tim.entity.commodity.Product;
import com.easidea.tim.entity.saleorder.SaleOrder;
import com.easidea.tim.entity.saleorder.SaleOrderItem;
import com.easidea.tim.util.Page;

public class SaleOrderItemService extends EntityService<SaleOrderItem, SaleOrderItemDao>{
	private SaleOrderItemDao saleOrderItemDao;

	public Page listSaleOrderItem(SaleOrder saleOrder,Product product,String creator,Date startDate,Date endDate,Page page){
		Criteria ca = this.listSaleOrderItemByCondition(saleOrder, product, creator, startDate, endDate);
		ca.setProjection(Projections.count("id"));
		Integer total = (Integer)ca.uniqueResult();
		int startIndex = Page.getStartOfPage(page.getCurrentPageNo(), page
				.getPageSize());
		ca.setFirstResult(startIndex);
		ca.setMaxResults(page.getPageSize());
		Page resultPage = new Page(startIndex,total,page.getPageSize(),ca.list());
		resultPage.setCurrentPageNo(page.getCurrentPageNo());
		return resultPage;
	}
	
	public Criteria listSaleOrderItemByCondition(SaleOrder saleOrder,Product product,String creator,Date startDate,Date endDate){
		Criteria ca = this.createUnDeletedCriteria();
		if(creator !=null){
			ca.add(Restrictions.eq("creator", creator));
		}
		if(startDate !=null){
			ca.add(Restrictions.ge("createTime", startDate));
		}
		if(endDate !=null){
			ca.add(Restrictions.le("createTime", DateUtils.addDays(endDate, 1)));
		}
		
		if(saleOrder !=null){
			ca.add(Restrictions.eq("saleOrder.id", saleOrder.getId()));
		}
		if(product !=null){
			ca.add(Restrictions.eq("product.id", product.getId()));
		}
		
		ca.addOrder(Order.desc("id"));
		return ca;
	}
	
	public boolean delSaleOrderOItem(SaleOrderItem saleOrderItem){
		SaleOrderItem saleOrderItemTmp = get(saleOrderItem.getId());
		saleOrderItemDao.remove(saleOrderItemTmp);
		return true;
	}
	
	public boolean updateSaleOrderItem(SaleOrderItem saleOrderItem){
		this.saleOrderItemDao.UpdateSaleOrderItem(saleOrderItem);
		return true;
	}
	
	public String isComplete(long orderId){
		String result = "2";
		List itemlist = this.saleOrderItemDao.getItemsByOrderId(orderId);
		for(int i = 0;i<itemlist.size();i++){
			SaleOrderItem saleOrderItem = (SaleOrderItem)itemlist.get(i);
			if(saleOrderItem.getMeterNum().trim().equals("0")){
				result = "1";
			}
		}
		
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List getItemsById(long id){
		return saleOrderItemDao.getItemsById(id);
	}
	
	@SuppressWarnings("unchecked")
	public List getItemsByOrderId(long id){
		return saleOrderItemDao.getItemsByOrderId(id);
	}
	public SaleOrderItemDao getSaleOrderItemDao() {
		return saleOrderItemDao;
	}

	public void setSaleOrderItemDao(SaleOrderItemDao saleOrderItemDao) {
		this.saleOrderItemDao = saleOrderItemDao;
	}
	
	
	@Override
	public SaleOrderItemDao getEntityDao() {
		// TODO Auto-generated method stub
		return null;
	}
}
